import Vue from 'vue';
import iView from 'iview';
import {router} from './router/index';
import {appRouter} from './router/router';
import store from './store';
import App from './app.vue';
import '@/locale';
import 'iview/dist/styles/iview.css';
import VueI18n from 'vue-i18n';
import util from '@/libs/util';
import Cookies from 'js-cookie';
import storage from '@/libs/storage';
import BaiduMap from 'vue-baidu-map';

Vue.use(VueI18n);
Vue.use(iView);
Vue.use(BaiduMap, {
    ak: util.getBMapKey()
});

Vue.config.productionTip = true;
Vue.config.devtools = true;

import Echo from 'laravel-echo'
import io from 'socket.io-client';
window.io = io;
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});


new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App),
    data: {
        currentPageName: '',
        currentModuleName: '',
    },
    mounted () {
        if (this.currentModuleName == '') {
            if (Cookies.get('currentModuleName')) {
                this.currentModuleName = Cookies.get('currentModuleName');
            } else {
                this.currentModuleName = util.getModuleMenuList()[0].name;
            }
        }
        this.$store.commit('setCurrentModuleName', this.currentModuleName);

        this.currentPageName = this.$route.name;
        this.$store.commit('initCachepage');
        // 权限菜单过滤相关
        this.$store.commit('updateMenulist', this.currentModuleName);
    },
    created () {
        let tagsList = [];
        appRouter.map((item) => {
            if (item.children.length <= 1) {
                tagsList.push(item.children[0]);
            } else {
                tagsList.push(...item.children);
            }
        });
        this.$store.commit('setTagsList', tagsList);

        this.$store.commit('setUser', storage.get('user'));
        this.$store.commit('setTokens', storage.get('tokens'));
    }
});

